package test;

/**
 * @Auther leiHua
 * @Date 2022/5/12 18:37
 * 轮播图效果
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Imag extends JFrame {
    MyJPanel mp;
    int index;

    ImageIcon[] imgs = {
            new ImageIcon("src/imag/1.jpg"),
            new ImageIcon("src/imag/15.jpg"),
            new ImageIcon("src/imag/logo.jpg"),
//            new ImageIcon("src/images/4.jpg"),
//            new ImageIcon("src/images/5.jpg"),
//            new ImageIcon("src/images/6.jpg"),
//            new ImageIcon("src/images/7.jpg"),
//            new ImageIcon("src/images/8.jpg"),
    };
    public Imag() {
        mp = new MyJPanel();
        this.add(mp);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("窗口");
        this.setVisible(true);
        Timer timer = new Timer(500,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mp.repaint();
            }
        });
        timer.start();

        //------image抽象类 getScaledInstance  缩放图像
        Image scaledInstance = imgs[0].getImage().getScaledInstance(300, 300, Image.SCALE_DEFAULT);

    }
    public static void main(String[] args) {
        new Imag();
    }
    class MyJPanel extends JPanel{
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.drawImage(imgs[index%imgs.length].getImage(), 0, 0,this);
            index++;
        }
    }
}
